package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.ShowTypeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Classname ShowTypeAttrLinkHandler
 * @Description 产品属性 初始不展示 ，点击更多展示，show_type=M
 *              此时属性联动由隐藏变为显示时，默认show_type=D，需要将 show_type=M
 *              显示由数据库sql配置实现，此处仅修改 show_type=M
 *
 *     注：该类目前仅修改 show_type=M，实际展示/隐藏、必填/非必填 由表配置实现
 * @Author zhang.song
 * @Date 2021-02-22 16:35
 */
@Service
@AttrHandler(ShowTypeAttrHandler.class)
public class ShowTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        String fieldName = attr.getFieldName();
        switch(fieldName) {
            case "2_0000016":
                // SLA标识=是，显示 SLA_业务开通、SLA_网络质量保证、SLA_售后服务，show_type 改为 M
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("2_0000013");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr1.getOriginValue());
                    }
                    KiteAttr attr2 = inst.getAttr("2_0000014");
                    if (Objects.nonNull(attr2)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr2, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr2.getOriginValue());
                    }
                    KiteAttr attr3 = inst.getAttr("2_0000015");
                    if (Objects.nonNull(attr3)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr3, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr3.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("2_0000013");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                    KiteAttr attr2 = inst.getAttr("2_0000014");
                    if (Objects.nonNull(attr2)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr2, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                    KiteAttr attr3 = inst.getAttr("2_0000015");
                    if (Objects.nonNull(attr3)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr3, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            case "200003068":
                // A端是否汇聚=是，显示 A端汇聚端口及时隙（210003979），show_type 改为 M
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("210003979");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("210003979");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            case "200003069":
                // Z端是否汇聚=是，显示 Z端汇聚端口及时隙（210003978），show_type 改为 M
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("210003978");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("210003978");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            case "2_0000023":
                // A端组网方式=汇聚，显示 A端汇聚电路业务号码（2_0000138），show_type 改为 M
                if ("1_0000175".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("2_0000138");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("2_0000138");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            case "2_0000024":
                // Z端组网方式=汇聚，显示 Z端汇聚电路业务号码（2_0000139），show_type 改为 M
                if ("1_0000175".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("2_0000139");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("2_0000139");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            case "2_0000043":
                // 是否接入在用网络=是，显示 在用电路代号（2_0000158），show_type 改为 M
                // 优化需求 是否接入在用网络=是，显示 在用电路代号（2_0000158），show_type 改为D  20211010-为否时在用电路默认添加一个0值，解决追单时选否不隐藏在用电路
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("2_0000158");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("2_0000158");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_H, "0");
                    }
                }
                break;
            case "200001767":
                // 是否BGP MD5认证=是，显示 BGP MD5密码（200001768），show_type 改为 M
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("200001768");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("200001768");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_H, "");
                    }
                }
                break;
            case "2_0000042":
                // 路由协议=BGP，显示AS OVERRIDE（20210309016），show_type 改为 M
                String [] linkAttrs = new String[]{"200004036", "200001767", "200001768", "200001765", "200001766", "200001727", "200001728"};
                for(String attrName : linkAttrs){
                    KiteAttr attr1 = inst.getAttr(attrName);

                    if (Objects.nonNull(attr1)) {
                        if(KiteStringUtils.equals(attr.getValue(),"1_0000213")){
                            if(KiteStringUtils.equals("200001768",attrName)){
                                KiteAttr attr_200001767 = inst.getAttr("200001767");
                                if(Objects.nonNull(attr_200001767)&&!KiteStringUtils.equals(attr_200001767.getValue(),"1_0000106")){
                                    //是否BGP MD5认证=否，隐藏 BGP MD5密码（200001768）
                                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_H, attr1.getOriginValue());
                                    continue;
                                }
                            }
                            if(StringUtils.isEmpty(attr1.getValue())){
                                if(StringUtils.isEmpty(attr1.getOriginValue())){
                                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getDefaultValue());
                                }else{
                                    LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                                }
                            }else{
                                LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getValue());
                            }
                        }else {
                            LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_H, "");
                        }
                    }
                }
                KiteAttr attr_200003778 = inst.getAttr("200003778");
                KiteAttr attr_200003779 = inst.getAttr("200003779");
                KiteAttr attr_2_0000047 = inst.getAttr("2_0000047");
                if(Objects.nonNull(attr_200003778)&&Objects.nonNull(attr_2_0000047)){
                    if(Objects.nonNull(attr_200003778)&&KiteStringUtils.equalsAny(attr_2_0000047.getValue(),"1_0000219","1_0000221")&&!KiteStringUtils.equals(attr.getValue(),"1_0000213")){
                        attr_200003778.setIsNull(KeyConsts.IFTRUE_F);
                    }else {
                        attr_200003778.setIsNull(KeyConsts.IFTRUE_T);
                    }

                    if(Objects.nonNull(attr_200003779)&&KiteStringUtils.equalsAny(attr_2_0000047.getValue(),"1_0000220","1_0000221")&&!KiteStringUtils.equals(attr.getValue(),"1_0000213")){
                        attr_200003779.setIsNull(KeyConsts.IFTRUE_F);
                    }else {
                        attr_200003779.setIsNull(KeyConsts.IFTRUE_T);
                    }
                }
                break;
            case "2_0000047":
                //IP协议版本和路由协议确定内网地址/掩码是否必填
                KiteAttr attr_200003778_1 = inst.getAttr("200003778");
                KiteAttr attr_200003779_1 = inst.getAttr("200003779");
                KiteAttr attr_2_0000042 = inst.getAttr("2_0000042");
                if("1_0000219".equals(attr.getValue())) {
                    // ipv4 隐藏ipv6相关属性
                    LocalAttrHelper.hideAndNullable(inst, "2_0000184", "2_0000185");
                    LocalAttrHelper.showAndNotNull(inst, "2_0000163", "2_0000167", "200003778");
                }
                else if ("1_0000220".equals(attr.getValue())) {
                    // ipv6 隐藏ipv4相关属性
                    LocalAttrHelper.showAndNotNull(inst, "2_0000184", "2_0000185");
                    LocalAttrHelper.hideAndNullable(inst, "2_0000163", "2_0000167", "200003778");
                }
                else if ("1_0000221".equals(attr.getValue())) {
                    // 双栈ipv4、ipv6相关属性均展示且必填
                    LocalAttrHelper.showAndNotNull(inst, "2_0000184", "2_0000185");
                    LocalAttrHelper.showAndNotNull(inst, "2_0000163", "2_0000167", "200003778");
                }
                if(Objects.nonNull(attr_2_0000042)){
                    //ipV4
                    if(Objects.nonNull(attr_200003778_1)&&KiteStringUtils.equalsAny(attr.getValue(),"1_0000219","1_0000221")&&!KiteStringUtils.equals(attr_2_0000042.getValue(),"1_0000213")) {
                        attr_200003778_1.setIsNull(KeyConsts.IFTRUE_F);
                    } else {
                        attr_200003778_1.setIsNull(KeyConsts.IFTRUE_T);
                    }
                    //ipV6
                    if(Objects.nonNull(attr_200003779_1)&&KiteStringUtils.equalsAny(attr.getValue(),"1_0000220","1_0000221")&&!KiteStringUtils.equals(attr_2_0000042.getValue(),"1_0000213")){
                        attr_200003779_1.setIsNull(KeyConsts.IFTRUE_F);
                    }else {
                        attr_200003779_1.setIsNull(KeyConsts.IFTRUE_T);
                    }
                }
                break;
            case "2_0000002":
                // 是否备份电路=是，显示主用电路ID（2_0000097），show_type 改为 M
                if ("1_0000106".equals(attr.getValue())) {
                    KiteAttr attr1 = inst.getAttr("2_0000097");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_M, attr1.getOriginValue());
                    }
                    String specId = inst.getSpecId();
                    if (KiteStringUtils.equalsAny(specId,"60040446","60040447","60040444")){
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_D, attr1.getOriginValue());
                    }
                }
                else {
                    KiteAttr attr1 = inst.getAttr("2_0000097");
                    if (Objects.nonNull(attr1)) {
                        LocalAttrHelper.hiddenOrDisplayProdAttr(data.getSceneInstId(), inst, attr1, KeyConsts.PROD_ATTR_SHOW_TYPE_E, "");
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }
}
